package com.jlnk.gsondemo.views;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jlnk.gsondemo.R;
import com.jlnk.gsondemo.entity.Book;
import com.jlnk.gsondemo.entity.Result;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BookDetailActivity extends AppCompatActivity {
    private static final String TAG = "BookDetailActivity";
    private ImageView bookImageView;
    private TextView bookTitleTextView;
    private TextView bookCatalogTextView;
    private TextView bookTagsTextView;
    private TextView bookSub1TextView;
    private TextView bookSub2TextView;
    private TextView bookReadingTextView;
    private TextView bookOnlineTextView;
    private TextView bookByTimeTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);

        initializeUI();

        String bookJson = getJson("book.json");
        Book book = parseBookJson(bookJson);
        updateUI(book);
    }
    private void initializeUI() {
        bookImageView = findViewById(R.id.book_image);
        bookTitleTextView = findViewById(R.id.book_title);
        bookCatalogTextView = findViewById(R.id.book_catalog);
        bookTagsTextView = findViewById(R.id.book_tags);
        bookSub1TextView = findViewById(R.id.book_sub1);
        bookSub2TextView = findViewById(R.id.book_sub2);
        bookReadingTextView = findViewById(R.id.book_reading);
        bookOnlineTextView = findViewById(R.id.book_online);
        bookByTimeTextView = findViewById(R.id.book_bytime);
    }

    private String getJson(String fileName) {
        StringBuilder json = new StringBuilder();
        try {
            InputStream is = getAssets().open(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null) {
                json.append(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    private Book parseBookJson(String json) {
        Gson gson = new Gson();
        Result result = gson.fromJson(json, Result.class);
        if (result != null && result.getResult() != null && result.getResult().getData() != null) {
            return result.getResult().getData();
        } else {
            return null;
        }
    }

    private void updateUI(Book book) {
        if (book != null) {
            bookTitleTextView.setText(book.getTitle());
            bookCatalogTextView.setText(book.getCatalog());
            bookTagsTextView.setText(book.getTags());
            bookSub1TextView.setText(book.getSub1());
            bookSub2TextView.setText(book.getSub2());
            bookReadingTextView.setText(book.getReading());
            bookOnlineTextView.setText(book.getOnline());
            bookByTimeTextView.setText(book.getBytime());

            String imgUrl = book.getImg();
            if (imgUrl != null && !imgUrl.isEmpty()) {
                Glide.with(this).load(imgUrl).into(bookImageView);
            }
        }
    }
}